/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package be.ua.ac.comp.projectGPT.auctionPolicies;

import be.ac.ua.comp.projectGPT.exceptions.InitializationException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 * Implementation for an auction policy on which the auction depends.
 * The auction will end after a certain amount of time.
 */
public class TimingPolicy extends AuctionPolicy implements ActionListener {

    private Timer timer;
    private boolean end;

    public TimingPolicy(int time) {
        this.timer = new Timer(time,this);
    }

    public boolean stop() throws InitializationException {
        if(!initialized) {
            throw new InitializationException();
        }
        return end;
    }

    @Override
    public void start() throws InitializationException {
        if(!initialized) {
            throw new InitializationException();
        }
        logger.info("Starting");
        end = false;
        timer.start();
    }

    /**
     * Will be called when the timer ends.
     */
    public void actionPerformed(ActionEvent e) {
        end = true;
        logger.info("Timer ended");
    }

}
